package com.threegold.listviewsample.slice;

import com.threegold.listview.ListView;
import com.threegold.listviewsample.ResourceTable;
import com.threegold.listviewsample.provider.CommonListItemProvider;
import ohos.aafwk.ability.AbilitySlice;
import ohos.aafwk.content.Intent;
import ohos.agp.components.Component;
import ohos.agp.components.LayoutScatter;

import java.util.ArrayList;
import java.util.List;

public class HeaderAndFooterAbilitySlice extends AbilitySlice {
    @Override
    public void onStart(Intent intent) {
        super.onStart(intent);
        super.setUIContent(ResourceTable.Layout_ability_header_and_footer);

        ListView listView = (ListView) findComponentById(ResourceTable.Id_listView);

        LayoutScatter layoutScatter = LayoutScatter.getInstance(this);

        // 加载Header
        Component headerLayout = layoutScatter.parse(ResourceTable.Layout_header, listView, false);
        // 加载Footer
        Component footerLayout = layoutScatter.parse(ResourceTable.Layout_footer, listView, false);
        // 创建适配器
        CommonListItemProvider itemProvider = new CommonListItemProvider(this);
        // Header和Footer都可以添加多个，展示顺序按照添加顺序，这里只展示添加一个
        listView.addHeaderView(headerLayout)
                .addFooterView(footerLayout)
                .setItemProvider(itemProvider);
        itemProvider.setDataList(getDatas());
    }

    private List<String> getDatas() {
        List<String> list = new ArrayList<>();
        for (int i = 0; i < 4; i++) {
            list.add("item" + i);
        }
        return list;
    }


}
